//
//  InviteListController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 2017/3/15.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "InviteListController.h"
#import "InviteExpendModel.h"
#import "InviteSecondListController.h"

@interface InviteListController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSUInteger _refreshIndex;
    UIImageView *noDrawImage;

}
@property(nonatomic,strong)NSMutableArray *firstArray;
@property(nonatomic,strong)MBProgressHUD *progressHUD;
@end

@implementation InviteListController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.title = @"我的邀请人";
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 55;
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.backgroundColor = BACKGROUND_COLOR;
    
    _refreshIndex = 1;
    [self loadInviteExpandUserListsData:_refreshIndex];
    
    self.tableView.mj_footer = [TopPullRefreshAnaimation footerWithRefreshingTarget:self refreshingAction:@selector(InviteExpandUserListLoadMoreData)];
}

- (void)InviteExpandUserListLoadMoreData
{
    _refreshIndex ++;
    [self loadInviteExpandUserListsData:_refreshIndex];
    [self.tableView.mj_footer endRefreshing];
}


- (void)loadInviteExpandUserListsData:(NSInteger)refreshIndex
{
    
    self.progressHUD = [MBProgressHUD show:self.view andMessage:nil];
    [self.view addSubview:_progressHUD];
    
    if (refreshIndex == 1) {
        [self.firstArray removeAllObjects];
    }
    NSString *refreshIndexStr = [NSString stringWithFormat:@"%zd",refreshIndex];
    
    WeakSelf(self);
    kW_DISPATCH_GLOBAL_QUEUE_DEFAULT((^{
        
        NSDictionary *params = @{@"username":USERNAME,@"page":refreshIndexStr};
        [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/Expand/userList" completeBlock:^(id object) {
            
            StrongSelf(self);
            if (JudgeBackDataCode) {
                NSArray *dataArray = [object objectForKey:@"data"];
                for (NSDictionary *dict in dataArray) {
                    InviteExpendModel *inviteExpendModel = [[InviteExpendModel alloc] initWithDictionary:dict error:nil];
                    if (inviteExpendModel) {
                        [self.firstArray addObject:inviteExpendModel];
                    }
                }
                
            }else{
                
                //订单没有
                if (refreshIndex == 1) {
                    
                    noDrawImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_NoRecord"]];
                    [self.view addSubview:noDrawImage];
                    
                    [noDrawImage mas_makeConstraints:^(MASConstraintMaker *make) {
                        make.centerX.equalTo(self.view.mas_centerX);
                        make.centerY.equalTo(self.view.mas_centerY).offset(-HEIGHT/8);
                        make.width.mas_equalTo(WIDTH/1.5);
                        make.height.mas_equalTo(WIDTH/2.4);
                    }];
                    
                }else{
                    
                    //没有更多数据
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                    
                }
                
            }
            
            kW_DISPATCH_MAIN_THREAD(^{
                
                [self.tableView reloadData];
                [self.progressHUD hide:true];
            });
        }];
    }));
}


#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.firstArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"InviteExpendCell"];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"InviteExpendCell"];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    InviteExpendModel *inviteExpendModel = self.firstArray[indexPath.section];
    cell.imageView.image = [UIImage imageNamed:@"icon_expand_user"];
    cell.textLabel.text = [NSString stringWithFormat:@"%@(%@)",inviteExpendModel.realname,inviteExpendModel.username];
    cell.textLabel.font = FONT(15);
    cell.detailTextLabel.text = inviteExpendModel.create_time;
    cell.detailTextLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:13];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    InviteSecondListController *InviteSecondVC = [[InviteSecondListController alloc] init];
    InviteExpendModel *inviteExpendModel = self.firstArray[indexPath.section];
    InviteSecondVC.secondUsername = inviteExpendModel.username;
    InviteSecondVC.title = [NSString stringWithFormat:@"%@的邀请人",inviteExpendModel.realname];
    [self.navigationController pushForm:self.navigationController pushToAndHidden:InviteSecondVC];
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (NSMutableArray *)firstArray
{
    if (!_firstArray) {
        _firstArray = [NSMutableArray array];
    }
    return _firstArray;
}


@end
